/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.user;

import sw6.digipecs.R;
import sw6.digipecs.helpers.AudioPlayer;
import sw6.digipecs.model.PECSBook;
import sw6.digipecs.widget.AnimationLayout;
import sw6.digipecs.widget.PageBarLayout;
import sw6.digipecs.widget.dragdrop.DragLayout;
import sw6.digipecs.widget.user.PECSBookPageView;
import sw6.digipecs.widget.user.SentenceBoardLayout;
import sw6.girafAppKit.GirafActivity;
import android.os.Bundle;

/**
 *  Main entrance point for digiPECS, this activity shows the book and allows
 *  creation of sentences.
 */
public class MainActivity extends GirafActivity {
	
	private PECSBook mBook;
	private SentenceBoardLayout mSentenceBoard;
	
	/**
	 *  Called, by Android, when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		// Get widgets we need
		PECSBookPageView pageView = (PECSBookPageView) findViewById(R.id.mainImgGrid);
		DragLayout dragLayout = (DragLayout) findViewById(R.id.mainDrag);
		AnimationLayout animationLayout = (AnimationLayout) findViewById(R.id.mainAnimation);
		mSentenceBoard = (SentenceBoardLayout) findViewById(R.id.sentenceboardView);
		PageBarLayout pagebar = (PageBarLayout) findViewById(R.id.pagebar);
		
		// Create book
		// TODO Change -1 to 0, when situations has been created!
		mBook = new PECSBook(this, true, 0);
		
		pageView.setBookAndDragLayout(mBook, dragLayout);
		pagebar.setBook(mBook);
		mSentenceBoard.setDragLayer(dragLayout);
		mSentenceBoard.setAnimationLayer(animationLayout);
		pageView.setAnimationLayer(animationLayout);
		
		
		//TODO Shake it
		/*final SensorManager sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
		final Sensor sensor = sensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
		sensorMgr.registerListener(new ShakeDetecter(this), sensor, SensorManager.SENSOR_DELAY_GAME);*/
	}

	/**
	 *  Called, by Android, when the activity is paused.
	 *  
	 *  This closes the book, which is then reopened by onResume(). 
	 */
	@Override
	protected void onPause() {
		mBook.close();
		AudioPlayer.close();
		mSentenceBoard.saveState();
		
		super.onPause();
	}

	/**
	 *  Called, by Android, when the activity is resumed.
	 *  
	 *  This opens the book, which may have never been open, or was previously
	 *  closed by onPause().
	 */
	@Override
	protected void onResume() {
		mBook.open();
		AudioPlayer.open();
		mSentenceBoard.restoreState();
		
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		mSentenceBoard.clearState();
		
		super.onDestroy();
	}
	
}